<?php
  /**
   * iJWhatToDo
   *
   * The iJWhatToDo class 
   *
   * @copyright Copyright (c) 2011, XBloks Progect
   */
    class WhatToDo extends iJBlock{
        
        /**
         *
         * Class Constructuctor
         * @access public
         */
        public function __construct() {
    
        }
        
        
        /**
         * execute
         *
         * Executes the block to generate the tokens
         *
         * @return array Array with the tokens of the block
         *
         * @access public
         */
        public function execute() {
          
          $search = isset($_REQUEST['search']) && !empty($_REQUEST['search']) ? $_REQUEST['search'] : '';
          $page   = isset($_REQUEST['page'])   && !empty($_REQUEST['page'])   ? $_REQUEST['page']   : 1;
          
          $toDoArray = array();
          $classes   = array();
          $classes   = array_merge(iJScanDir(DIR_CLASSES), iJScanDir(DIR_BLOCKS), iJScanDir(DIR_STATIC_CLASSES));
          foreach ($classes as $className => $path){
            if($className == 'WhatToDo') {
              continue;
            }
            $content = file_get_contents($path, FILE_TEXT);
            $pattern = '/\/\/TODO(.*)\n/isUS';
            $pos = 1;
            while(preg_match($pattern, $content, $matches)) {
              if (!is_null($matches[0])) {
                $cut     = strpos($content, '//TODO') + 1;
                $textBef = substr($content, 0, $cut);
                $content = substr($content, $cut);
                $pos     = substr_count($textBef, "\n") + $pos;
                
                
                $temp['className'] =  $className;
                $temp['toDo']      =  substr($matches[0], 0, strlen($matches[0]) - 1);
                $temp['pos']       =  $pos;
                
                if($search != '') {
                  $search = strtolower($search);
                  if((substr_count(strtolower($temp['className']), $search) != 0) ||  (substr_count(strtolower($temp['toDo']), $search) != 0)) {
                    $toDoArray[] = $temp;
                  }
                } else {
                  $toDoArray[] = $temp;
                }
                
              }
            }
            
          }
          
          $pageArray = array();
          $index = RESULT_LIMIT * ($page-1);
          
          for($i = 0; $i < RESULT_LIMIT; $i++) {
            if(!isset($toDoArray[$index + $i])) {
               break;
            }
            $pageArray[$i] = $toDoArray[$index + $i];
          }
          
          if($page <= 1) {
            $this->addToken('prev_link', iJHrefLink::linkToLocalPage('', iJHrefLink::Icon('previous_disabled', '32x32')));
          } else {
            $get['page'] = $page - 1;
            $this->addToken('prev_link', iJHrefLink::linkToLocalPage('admin_todo', iJHrefLink::Icon('previous', '32x32'), $get));
          }
          
          if($page >= ceil(count($toDoArray) / RESULT_LIMIT)) {
            $this->addToken('next_link', iJHrefLink::linkToLocalPage('', iJHrefLink::Icon('next_disabled', '32x32')));
          } else {
            $get['page'] = $page + 1;
            $this->addToken('next_link',  iJHrefLink::linkToLocalPage('admin_todo', iJHrefLink::Icon('next', '32x32'), $get));
          }
          
          $this->addToken('form_action', iJHrefLink::linkToLocalPage('admin_todo')); 
          $this->addToken('icon',        iJHrefLink::Icon('page_code', '16x16'));
         
          $this->addToken('search',      $search);
          $this->addToken('page',        $page);
          $this->addToken('array',       $pageArray);
          
          $this->addToken('total_items',    count($toDoArray));
          $this->addToken('pages_count',    ceil(count($toDoArray) / RESULT_LIMIT));

          return parent::execute();
        }
        
    }
?>